import React from 'react'
import dayjs from 'dayjs'
import classNames from 'classnames'
import avatar from './avatar.png'

export default class componentName extends React.Component {
  state = {
    // hot: 热度排序  time: 时间排序
    tabs: [
      {
        id: 1,
        name: '热度',
        type: 'hot',
      },
      {
        id: 2,
        name: '时间',
        type: 'time',
      },
    ],
    active: 'time',
    list: [
      {
        id: 1,
        author: '刘德华',
        comment: '给我一杯忘情水',
        time: new Date('2021-11-10 09:09:00'),
        img: 'https://y.qq.com/music/photo_new/T001R300x300M000003aQYLo2x8izP.jpg?max_age=2592000',
        // 1: 点赞 0：无态度 -1:踩
        attitude: 1,
      },
      {
        id: 2,
        author: '周杰伦',
        comment: '听妈妈的话',
        time: new Date('2021-12-11 09:09:00'),
        img: 'https://y.qq.com/music/photo_new/T001R500x500M0000025NhlN2yWrP4.jpg?max_age=2592000',
        // 1: 点赞 0：无态度 -1:踩
        attitude: 0,
      },
      {
        id: 3,
        author: '陈奕迅',
        comment: '十年',
        time: new Date('2021-10-11 10:09:00'),
        img: 'https://y.qq.com/music/photo_new/T001R500x500M000003Nz2So3XXYek.jpg?max_age=2592000',
        // 1: 点赞 0：无态度 -1:踩
        attitude: -1,
      },
    ],
    comment: '',
  }
  // 切换
  handleChangeTab(item) {
    // item 就是每一项数据
    return (e) => {
      this.setState({ active: item.type })
    }
  }
  // 删除
  handleDel(item) {
    this.setState({
      list: this.state.list.filter((i) => i.id !== item.id),
    })
  }
  // 渲染列表
  renderList() {
    return this.state.list.length === 0 ? (
      <h3 style={{ textAlign: 'center' }}>暂无评论~~</h3>
    ) : (
      this.state.list.map((item) => (
        <div className='list-item' key={item.id}>
          <div className='user-face'>
            <img className='user-head' src={item.img} alt='' />
          </div>
          <div className='comment'>
            <div className='user'>{item.author}</div>
            <p className='text'>{item.comment}</p>
            <div className='info'>
              <span className='time'>
                {dayjs(item.time).format('YYYY-MM-DD HH:mm:ss')}
              </span>
              <span
                className={classNames('like', {
                  liked: item.attitude === 1,
                })}
              >
                <i className='icon'></i>
              </span>
              <span
                className={['hate', item.attitude === -1 ? 'hated' : ''].join(
                  ' '
                )}
              >
                <i className='icon'></i>
              </span>
              <span
                className='reply btn-hover'
                onClick={this.handleDel.bind(this, item)}
              >
                删除
              </span>
            </div>
          </div>
        </div>
      ))
    )
  }
  // 添加
  handleSubmit = () => {
    const o = {
      id: Date.now(),
      author: '桂升',
      comment: this.state.comment,
      time: new Date(), // 要么在这儿把日期对象转成普通字符串
      img: avatar,
      attitude: 0,
    }
    this.setState({ list: [o, ...this.state.list], comment: '' })
  }
  render() {
    const { state } = this
    return (
      <div className='App'>
        <div className='comment-container'>
          <div className='comment-head'>
            <span>{state.list.length} 评论</span>
          </div>
          <div className='tabs-order'>
            <ul className='sort-container'>
              {/* 只有 state.active 等于 item.type 的时候才使用高亮的 class on */}
              {/* onClick={(e) => this.handleChangeTab(item, e)} */}
              {/* onClick={this.handleChangeTab.bind(this, item)} */}
              {state.tabs.map((item) => (
                <li
                  className={classNames({
                    // 当右边的条件成立的时候应用左边的 class
                    on: state.active === item.type,
                  })}
                  key={item.id}
                  onClick={this.handleChangeTab(item)}
                >
                  按{item.name}排序
                </li>
              ))}
            </ul>
          </div>
          <div className='comment-send'>
            <div className='user-face'>
              <img className='user-head' src={avatar} alt='' />
            </div>
            <div className='textarea-container'>
              <textarea
                value={this.state.comment}
                onChange={(e) => this.setState({ comment: e.target.value })}
                cols='80'
                rows='5'
                placeholder='发条友善的评论'
                className='ipt-txt'
              ></textarea>
              <button className='comment-submit' onClick={this.handleSubmit}>
                发表评论
              </button>
            </div>
            <div className='comment-emoji'>
              <i className='face'></i>
              <span className='text'>表情</span>
            </div>
          </div>
          <div className='comment-list'>{this.renderList()}</div>
        </div>
      </div>
    )
  }
}
